package leetcode.editor.cn;

/**
 * @author: yangwk
 * @date: 2023/1/16
 * @description:
 **/
public class P1813SentenceSimilarityIII {
    public static void main(String[] args) {
        System.out.println(areSentencesSimilar("Eating right now", "Eating"));
    }

    public static boolean areSentencesSimilar(String sentence1, String sentence2) {
        String shortStr = sentence1, longStr = sentence2;
        if (sentence1.length() > sentence2.length()) {
            shortStr = sentence2;
            longStr = sentence1;
        }
        String[] shortArr = shortStr.split(" ");
        String[] longArr = longStr.split(" ");
        int shortStart = 0;
        int longStart = 0;
        int shortEnd = shortArr.length - 1;
        int longEnd = longArr.length - 1;
        while (shortStart <= shortEnd) {
            if (shortArr[shortStart].equalsIgnoreCase(longArr[longStart])) {
                shortStart++;
                longStart++;
            } else {
                if (shortArr[shortEnd].equalsIgnoreCase(longArr[longEnd])) {
                    shortEnd--;
                    longEnd--;
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}
